/*设置页面*/
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:frametrim/app/app_navigator.dart';
import 'package:frametrim/appviews/systemView1.dart';
import 'package:frametrim/netconnect/apiUrls.dart';
import 'package:frametrim/utility/deviceUtils.dart';
import 'package:frametrim/utility/sq_color.dart';
import 'package:frametrim/views/appbar_black.dart';
import 'package:frametrim/views/button_circle.dart';
import 'package:frametrim/views/dialogs/commonDialog.dart';
import 'package:frametrim/views/dialogs/loadingDialog.dart';
import 'package:package_info/package_info.dart';
import 'package:path_provider/path_provider.dart';

class SystemPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => SystemState();
}

class SystemState extends State<SystemPage> {
  PackageInfo _packageInfo = PackageInfo(
    appName: 'Unknown',
    packageName: 'Unknown',
    version: 'Unknown',
    buildNumber: 'Unknown',
  );

  //缓存
  late double cacheSize = 0.0;

  @override
  void initState() {
    super.initState();
    //版本信息相关
    getpackageInfo();
    //缓存
    getCacheInfo();
  }

  Future<void> getpackageInfo() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    setState(() {
      _packageInfo = packageInfo;
    });
  }

  /*获取缓存*/
  Future<void> getCacheInfo() async {
    //缓存相关
    Directory tempDir = await getTemporaryDirectory();
    double value = await DeviceUtils.getTotalSizeOfFilesInDir(tempDir);
    setState(() {
      cacheSize = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: Column(
          children: <Widget>[
            AppbarCenterTitle(
              title: '设置',
              imgStr: 'image/imv_back.png',
              titleColor: SQColor.black,
            ),
            Column(
              children: <Widget>[
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    AppNavigator.pushWeb(context, ApiUrs.h5AccSafe);
                  },
                  child: SystemViewWithText(
                    nameLeft: '账户与安全',
                  ),
                ),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    AppNavigator.pushWeb1(
                        context, ApiUrs.h5UserAgreen, '用户协议与隐私协议');
                  },
                  child: SystemViewWithText(
                    nameLeft: '用户协议与隐私协议',
                  ),
                ),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {},
                  child: SystemViewWithText(
                    nameLeft: '当前版本',
                    nameRight: 'V' + _packageInfo.version,
                  ),
                ),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: createCacheDialog,
                  child: SystemViewWithText(
                    nameLeft: '清理缓存',
                    nameRight: DeviceUtils.renderSize(cacheSize),
                  ),
                ),
              ],
            ),
            Expanded(child: Container()),
            ButtonCircleView('退出登录', SQColor.white, SQColor.color_3C64F9, exit),
            SizedBox(
              height: 30,
            )
          ],
        ),
      ),
    );
  }

  void exit() {
    createExitDialog();
  }

/*加载中弹窗*/
  void createDialog() {
    showDialog<void>(
      context: context,
      barrierDismissible: true, //是否可触摸取消
      builder: (BuildContext dialogContext) {
        return LoadingDialog(text: '加载中');
      },
    );
  }

  void dismissDialog() {
    Navigator.pop(context); //关闭对话框
  }

/*退出登录*/
  void createExitDialog() {
    showDialog<void>(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext dialogContext) {
        return CommonDialog(
          title: '提示',
          content: '您确定要退出登录吗？',
          button_left: '取消',
          onPress: () {
            print('退出登录');
          },
        );
      },
    );
  }

  /*清理缓存*/
  void createCacheDialog() {
    showDialog<void>(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext dialogContext) {
        return CommonDialog(
          title: '提示',
          content: '确定要清除缓存吗？',
          button_left: '取消',
          onPress: () {
            DeviceUtils.clearCache();
            dismissDialog();
            setState(() {
              cacheSize=0;
            });
          },
        );
      },
    );
  }
}
